/**
 * Historico de alteração
 *
 * Data         Autor   Descrição
 * 24/10/2010   Jerry   Criação do cabeçalho.
 *
 */

package Instrumentos;

/**
 *
 * @author Bruno
 */
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;

public class GraficoVelocidade extends ApplicationFrame implements ChartMouseListener {
    
    private XYSeries serie;
    private XYSeriesCollection dataset;
    private NumberAxis domain;
    private NumberAxis range;
    private double contador = 0;
    //private int p1, p2;
    
    public GraficoVelocidade(final String title) {
        
        super(title);
        serie = new XYSeries("Velocidade");
        dataset = new XYSeriesCollection(serie);
        
        JFreeChart chart = ChartFactory.createXYLineChart(
            "Velocidade",          // title
            "Tempo(s)",                 // x-axis label
            "Velocidade",              // y-axis label
            dataset,                    // data
            PlotOrientation.VERTICAL,   // orientation
            false,                      // create legend?
            true,                       // generate tooltips?
            false                       // generate URLs?
        );
        chart.setBackgroundPaint(new Color(224, 223, 227));
        
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.setOutlinePaint(Color.black);
        plot.setAxisOffset(new RectangleInsets(20.0, 20.0, 20.0, 20.0));
        //plot.setDomainCrosshairVisible(true);
        //plot.setRangeCrosshairVisible(true);
        
        domain = (NumberAxis)plot.getDomainAxis();
        domain.setAutoRange(false);
        domain.setRange(0, 35);
        
        range = (NumberAxis)plot.getRangeAxis();
        range.setAutoRange(false);
        range.setRangeWithMargins(0, 100);
        
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, true);
        
        setContentPane(chartPanel);
        
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, true);
        renderer.setSeriesShapesVisible(0, false);
        
        plot.setRenderer(renderer);
        
    }
    
    public NumberAxis getDomain() {
        return domain;
    }
    
    public XYSeries getSerie() {
        return serie;
    }
    
    public void add(double data) {
        serie.add(contador, data);

        contador++;

        if(contador == 10000) {
            contador = 0;
            serie.clear();
            domain.setAutoRange(false);
            domain.setRange(0, 35);
            range.setAutoRange(false);
            range.setRangeWithMargins(0, 100);
        }
    }
    
    public void abs() {
        
        /*int lowerBound = chartPanel.getLowerBound();
        int upperBound = chartPanel.getUpperBound();
        
        for(int i = lowerBound; i < upperBound; i++) {
            if(serie.getY(i).doubleValue() < 0 )
                serie.updateByIndex(i, serie.getY(i));
        }*/
        
    }
    
    public double rms() {
        
        /*double somatorio = 0;
        int lowerBound = chartPanel.getLowerBound();
        int upperBound = chartPanel.getUpperBound();
        
        for(int i = lowerBound; i < upperBound; i++)
            somatorio += serie.getY(i).doubleValue();
        
        System.out.println(Math.sqrt( somatorio/ (upperBound - lowerBound) );
        
        return Math.sqrt( somatorio / (upperBound - lowerBound) );*/
        return 0;
    }
    
    public void chartMouseClicked(ChartMouseEvent e) {
        
        ChartEntity chartEntity = e.getEntity();
        System.out.println("opa");
        if(chartEntity != null) {
            
            //if(chartEntity instanceof XYItemEntity) {
            XYItemEntity entity = (XYItemEntity)chartEntity;
            //seriesIndex = entity.getSeries(); //returns the series index for the selected point
            int itemIndex = entity.getItem();
            System.out.println(itemIndex);
            /*XYSeriesCollection oldDataset = (XYSeriesCollection)this.chart.getXYPlot().getDataset();
            
            //get the total number of series in the dataset
            int seriesNum = oldDataset.getSeriesCount();
            System.out.println("The total series number in the plot is: " + seriesNum);

            //get the selected XYSeries
            XYSeries s = oldDataset.getSeries(seriesIndex);
            s.delete(itemIndex, itemIndex);

            //get the total number of items in the selected series
            int itemNum = oldDataset.getItemCount(seriesIndex);
            System.out.println("The current number of points in series " + seriesIndex + " is: " + itemNum);*/

          //end inner if
            
        }
        
    }
    
    public void chartMouseMoved(ChartMouseEvent e) {}
    
    /*protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        //Nao podemos alterar o estado do objeto g, portanto fazemos uma copia dele.
        Graphics2D g2d = (Graphics2D)g.create();
        g2d.setColor(Color.red);
        
        //Percorre a nossa lista, pintando os pontos.
        for(int i = 0; i < getPointList().size(); i++) {
            
            PontoDouble point = getPointList().get(i);
            
            g2d.drawOval((int)point.getX()-1 , (int)point.getY()-1, 3, 3);
            
            if(isMostrarNumero())
                g2d.drawString("" + (i + 1), (int)point.getX(), (int)point.getY());
                
        }
        
        //E aqui destruimos a copia do objeto g. 
        //Nao se preocupe. O desenho feito por ele nao ser destruido.
        g2d.dispose();
        
    }*/
    
}

